<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\CardLogo;

class CardLogoRepository
{
    /**
     * 创建卡logo
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return CardLogo::create($params);
    }

    public function update($id, $params)
    {
        return CardLogo::where('id', $id)->update($params);
    }

    public function byId($id)
    {
        return CardLogo::find($id);
    }

    public function destroy($id)
    {
        return CardLogo::where('id', $id)->delete();
    }

    /**
     * 根据id获取卡logo资料
     * @param $id
     * @return mixed
     */
    public function findToBankCode($code)
    {
        return CardLogo::where('bank_code', $code)->first();
    }

    public function getCardList($request)
    {
        $page = $request->pageNumber - 1;
        $page = $page < 0 ? 0 : $page;
        $limit = $request->pageSize ? $request->pageSize : 50;
        $offset = $page * $limit;
        $query = CardLogo::where(function ($query) use ($request) {
                if (!empty($request->keyword)) {
                    switch ($request->field) {
                        case 'bank_code':
                            $query->where('bank_code', 'like', '%' . $request->keyword . '%');
                            break;
                        case 'bank_description':
                            $query->where('bank_description', 'like', '%' . $request->keyword . '%');
                            break;
                    }
                }
            });
        $total = $query->count();
        $result = $query->latest('id')
            ->offset($offset)
            ->limit($limit)
            ->get();
        return ['result' => $result, 'count' => $total];
    }
}